<?php 
require_once("admin-header.php");
require_once("../include/bm_config.php");
?>
<title>Contest List</title>
<center><h2>Contest List</h2></center>
<?php
require_once("../include/set_get_key.php");
$sql="SELECT max(`bm_c_id`) as upid, min(`bm_c_id`) as btid  FROM `bm_contest`";
$page_cnt=50;
$result=mysql_query($sql);
echo mysql_error();
$row=mysql_fetch_object($result);
$base=intval($row->btid);
$cnt=intval($row->upid)-$base;
$cnt=intval($cnt/$page_cnt)+(($cnt%$page_cnt)>0?1:0);
if (isset($_GET['page'])) 
	$page=intval($_GET['page']);
else
	$page=$cnt;
$pstart=$base+$page_cnt*intval($page-1);
$pend=$pstart+$page_cnt;
for ($i=1;$i<=$cnt;$i++)
{
	if ($i>1) echo '&nbsp;';
	if ($i==$page) echo "<span class=red>$i</span>";
	else echo "<a href='contest_list.php?page=".$i."'>".$i."</a>";
}
$sql="select `bm_c_id`,`bm_c_title`,`bm_c_start`,`bm_c_end`,`bm_c_startreg`,`bm_c_endreg`,`bm_c_defunct`,`bm_c_kind` FROM `bm_contest` where bm_c_id>=$pstart and bm_c_id <=$pend order by `bm_c_id` desc";

$result=mysql_query($sql) or die(mysql_error());
echo "<center><table width=90% border=1>";
echo "<tr><td>ContestID<td>Title[报名信息]<td>StartReg<td>EndReg<td>Status<td>Kind<td>Edit<td>Copy<td>Delete"; 
echo "</tr>";
for (;$row=mysql_fetch_object($result);){
	echo "<tr>";
	echo "<td><a href='../contest.php?cid=$row->bm_c_id'>".$row->bm_c_id."</a>";
	echo "<td><a href='../results.php?cid=$row->bm_c_id'>".$row->bm_c_title."</a>";
	echo "<td>".$row->bm_c_startreg;
	echo "<td>".$row->bm_c_endreg;
	$cid=$row->bm_c_id;
	if(isset($_SESSION['administrator']) || isset($_SESSION[$BM_ADMIN]))
	{
		echo "<td><a href=contest_df_change.php?cid=$row->bm_c_id&getkey=".$_SESSION['getkey'].">".($row->bm_c_defunct=="N"?"<span class=green>Available</span>":"<span class=red>Reserved</span>")."</a>";
		echo "<td><a href=contest_pr_change.php?cid=$row->bm_c_id&getkey=".$_SESSION['getkey'].">".($row->bm_c_kind=="0"?"组队赛->个人赛":"个人赛->组队赛")."</a>";
		echo "<td><a href=contest_edit.php?cid=$row->bm_c_id>Edit</a>";
		echo "<td><a href=contest_add.php?cid=$row->bm_c_id>Copy</a>";
		echo "<td><button onclick='info_delete(".$row->bm_c_id.")'>删除</button></td></tr>";
	}
	echo "</tr>";
}
echo "</table></center>";
?>

<script type="text/javascript">
function info_delete(cid)
{
	if(confirm("建议使用status，若删除比赛，则所有相关信息将无法恢复，确定删除？"))
	{
		window.location.href = <?php
		echo "\"./info_delete.php?cid=\"+cid+\"&oper=1&getkey=".$_SESSION['getkey']."\";";
		?>
		
	}
	else
  	{
		return false;
	}
}
</script>